<?php
/**
 * Created by PhpStorm.
 * User: xuzhong
 * Date: 2025/4/2
 * Time: 13:49
 */
namespace app\admin\service;

use app\admin\common\ServiceResponse;
use app\admin\model\Images;
use app\admin\model\Advertisement as AdvertisementModel;
use think\facade\Db;

class Advertisement extends BaseService
{
    use ServiceResponse;

    protected $model;

    public function __construct()
    {
        $this->model = new AdvertisementModel();
        parent::__construct();
    }

    /**
     * @describe 获取销售人员列表
     * @author: 徐众
     * @date: 2025-04-02 13:02:26
     * @param [] type
     * @return Renderable
     */
    public function getList($where, $page, $limit): array
    {
        if (0 >= $page || 100 < $limit) {
            return $this->error(100002);
        }

        $total = $this->model->where($where)->count();
        if (!$total) {
            return $this->success(['list' => [], 'total' => 0]);
        }
        $list = $this->model->where($where)
            ->page($page, $limit)
            ->order('create_time desc')
            ->select()->append(['position_value'])->toArray();
        $avatorId = array_unique(array_column($list,'image'));
        #获取图片url
        $oImages = new Images();
        $avatorUrls = $oImages->getImagesByIds($avatorId);
        $avatorUrls = array_column($avatorUrls, null, 'id');
         foreach ($list as $k=>$v) {
             $list[$k]['image'] = $avatorUrls[$v['image']]['url']??'';
             $list[$k]['imageName'] = $avatorUrls[$v['image']]['name']??'';
             $list[$k]['imageId'] = $avatorUrls[$v['image']]['id']??'';

         }
        return $this->success(['list' => $list, 'total' => $total]);
    }
    /**
     * 创建销售人员
     * @param array $data 创建物流公司的数据
     */
    public function create($data)
    {
        if(!empty($data)){
            try {

                $res = $this->model->create($data);
                if ($res) {
                    $this->logger->logOperation($res->id,'Minerpool','创建','创建矿池');
                    return $this->success();
                } else {
                    return $this->error(500001);
                }
            } catch (\Throwable $th) {
                return $this->error(500001, '服务器内部错误：' . $th->getMessage());
            }
        }else{
            return $this->error(500001, '服务器内部错误' );
        }

    }
    /**
     * 获取销售人员详情
     * @param int $id 物流公司ID
     */
    public function getOneById($id)
    {
        $customerContact = $this->model->find($id);
        if ($customerContact) {
            $data = $customerContact->append(['creator_value']);
        }
        $data = $data->toArray();
        return $this->success($data);
    }
    /**
     * 更新销售人员
     * @param int $id 物流公司ID
     * @param array $data 更新物流公司的数据
     */
    public function update($data)
    {
        $res = $this->model->where('id', $data['id'])->find();
        if (!$res) {
            return false;
        }
        foreach ($data as $key => $value) {
            if ($res->$key != $value) {
                $res->$key = $value;
            }
        }
        $result = $res->save();
        if ($result) {
            $this->logger->logOperation($res->id,'Minerpool','更新','更新矿池');
            return $this->success();
        } else {
            return $this->error(500001);
        }
    }

    /**
     * @describe 删除销售人员
     * @author: 徐众
     * @date: 2025-04-02 13:02:26
     * @param [] type
     * @return Renderable
     */
    public function delete($param): array
    {
        #校验参数
        if (!isset($param['ids']) || empty($param['ids'])) {
            return $this->error(100001, '操作失败');
        }
        #查询数据是否存在
        $result =$this->model->where('id', 'in', $param['ids'])->where('is_del', '=', 0)->select()->toArray();

        if (!empty($result)) {
            #更新记录
            $update = $this->model->where('id', 'in', $param['ids'])->where('is_del', '=', 0)->update([
                'is_del' => time()
            ]);
            if ($update) {
                #尝试记录操作日志 不影响主流程
                try {
                    $this->logger->logOperation($param['ids'], 'Minerpool', '删除', '删除矿池');
                } catch (\Exception $e) {
                    #记录日志或其他操作
                }
                return $this->success();
            } else {
                return $this->error(500001, '操作失败');
            }

        } else {
            #未找到记录返回失败
            return $this->error(400000, '未找到相关数据');
        }
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2025-02-13 11:03:36
     * @param [type] @required
     * @return Renderable
     */
    public function getFieldEnum($field = 'all')
    {
        $res = $this->model->getFieldEnum($field);
        return [
            'code' => 0,
            'data' => $res
        ];
    }
}